package com.zxjbyte.yiyi.extension.socket.config;

import com.zxjbyte.yiyi.extension.socket.handler.YiyiWebSocketHandler;
import com.zxjbyte.yiyi.extension.socket.interceptor.YiyiHandshakeInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @Author zhangxingjia
 * @Date 2024/7/14 21:13
 * @Version: 1.0
 */
@Configuration
@ConditionalOnProperty(name = "yiyi.websocket.enabled", havingValue = "true", matchIfMissing = true) //默认开启
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer{

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new YiyiWebSocketHandler(), "/ws")
                .addInterceptors(new YiyiHandshakeInterceptor())
                .setAllowedOrigins("*"); //允许来自任何源的 WebSocket 连接, 主要解决跨域问题
    }

}
